package com.nefu.softlab.xjwc.utils;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * @author : Ar1es
 * @date : 2020/1/18
 * @since : Java 8
 *  调用openoffice服务线程
 *  soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
 *  后台开启
 *  nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
 *  ps -ef | grep soffice
 *
 *  https://blog.csdn.net/brenda2314/article/details/95474191
 *
 */
@Slf4j
public class OfficeUtil {

    //private static ThreadLocal<OpenOfficeConnection> threadLocal = new ThreadLocal<>();

    /**
     * 将文件转化为PDF或者Html格式
     * @param sourceFile
     * @param format
     * @throws Exception
     */
    public static String File2PDFOrHtml(String sourceFile, String format) throws Exception{
        if (!"pdf".equals(format) && !"html".equals(format)) {
            throw new XJWCException("转化格式有误，请重试");
        }
        // 源文件目录
        File inputFile = new File(sourceFile);
        if (!inputFile.exists()) {
            log.info("源文件不存在");
            throw new XJWCException("源文件不存在");
        }
        // 输出文件目录
        String newFile = sourceFile.substring(0, sourceFile.lastIndexOf(".") + 1).concat(format);
        File outputFile = new File(newFile);
        // 连接openoffice服务118.24.41.50140.143.194.109

        //OpenOfficeConnection connection = threadLocal.get();
        //OpenOfficeConnection connection = new SocketOpenOfficeConnection("140.143.194.109", 8100);
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        connection.connect();
        System.out.println("连接成功!");

//        if (null == connection) {
//            connection = new SocketOpenOfficeConnection("140.143.194.109", 8100);
//            threadLocal.set(connection);
//            connection.connect();
//        }

        //OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

        // 这种会导致文字和图片丢失
        // DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(inputFile, outputFile);
        System.out.println("转换成功!");
        // 关闭连接
        connection.disconnect();
        System.out.println("关闭连接!");
        log.info("转换为" + format + "格式成功，路径为 " + outputFile.getPath());
        return outputFile.getPath();
    }


    public static void main(String[] args) throws Exception {
        String srcPath = "G:\\lxw\\excel.xlsx";

        OfficeUtil.File2PDFOrHtml(srcPath, "pdf");

        //OfficeUtil.File2PDFOrHtml("G:\\lxw\\林业可视化中期.ppt", "pdf");

        //OfficeUtil.File2PDFOrHtml("G:\\lxw\\2.docx", "pdf");
    }


}
